//
//  BoZeMainVC.swift
//  BoZeCalendar
//
//  Created by 李东 on 2024/4/10.
//

import UIKit
import SnapKit

class BoZeMainVC: BoZeBaseVC {

    lazy var scrollView = UIScrollView()
    lazy var setupBtn = UIButton(type: .custom)
    lazy var topImgView = UIImageView(image: UIImage(named: "img_home_top"))
    lazy var itemBtn1 = UIButton(type: .custom)
    lazy var itemBtn2 = UIButton(type: .custom)
    lazy var itemBtn3 = UIButton(type: .custom)
    
    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showBgImgView = true
        configUI()
        NotificationCenter.default.addObserver(self, selector: #selector(showAd), name: .SDKInitFinish, object: nil)
    }
    
    @objc func showAd() {
        if AppProducts.shared.isPurchased {
            adHelper.removeBannerView()
        }else{
            if hasLoadAd {
                return
            }
            hasLoadAd = true
            adHelper.loadBanner(currentVC: self, isMainVC: false)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if ADManager.initFinish {
            showAd()
        }
    }
    
    func configUI() {
        scrollView.contentInsetAdjustmentBehavior = .never
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        setupBtn.setImage(UIImage(named: "img_setup"), for: .normal)
        setupBtn.addTarget(self, action: #selector(homeHandle(sender:)), for: .touchUpInside)
        scrollView.addSubview(setupBtn)
        setupBtn.snp.makeConstraints { make in
            make.top.equalTo(CGFloat.topPadding)
            make.height.width.equalTo(44)
            make.right.equalTo(-10)
            make.left.equalTo(CGFloat.screenWidth-54)
        }
        
        scrollView.addSubview(topImgView)
        topImgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(setupBtn.snp.bottom)
        }
        
        itemBtn1.setImage(UIImage(named: "img_home_item1"), for: .normal)
        itemBtn1.addTarget(self, action: #selector(homeHandle(sender:)), for: .touchUpInside)
        scrollView.addSubview(itemBtn1)
        itemBtn1.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(topImgView.snp.bottom).offset(30)
        }
        
        itemBtn2.setImage(UIImage(named: "img_home_item2"), for: .normal)
        itemBtn2.addTarget(self, action: #selector(homeHandle(sender:)), for: .touchUpInside)
        scrollView.addSubview(itemBtn2)
        itemBtn2.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(itemBtn1.snp.bottom).offset(10)
        }

        itemBtn3.setImage(UIImage(named: "img_home_item3"), for: .normal)
        itemBtn3.addTarget(self, action: #selector(homeHandle(sender:)), for: .touchUpInside)
        scrollView.addSubview(itemBtn3)
        itemBtn3.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(itemBtn2.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-110)
        }

    }


    @objc func homeHandle(sender: UIButton) {
        if sender == setupBtn {
            navigationController?.pushViewController(BoZeMeVC(), animated: true)
            return
        }
        if !AppProducts.shared.isPurchased  {
            showSubVC()
            return
        }

        if sender == itemBtn1 {
            navigationController?.pushViewController(BoZeWebVC(url: AppConfig.H5_URL), animated: true)
        }else if sender == itemBtn2 {
            navigationController?.pushViewController(BoZeWebVC(url: AppConfig.H5_OLD_URL), animated: true)
        }else {
            navigationController?.pushViewController(BoZeCarListVC(), animated: true)
        }
    }

}
